//
//  MLZPlayLastViewController.m
//  MotherLover
//
//  Created by zj on 16/1/1.
//  Copyright © 2016年 zj. All rights reserved.
//

#import "MLZPlayLastViewController.h"
#import "MLZPlayLastViewCell.h"
#import "zJRequestManager.h"
#import "MLZPlistLastModel.h"
#import "JRPlayerViewController.h"
@interface MLZPlayLastViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *listArray;
@property (nonatomic, assign)NSInteger index;

@end

@implementation MLZPlayLastViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"%@",_zjId);
    self.view.backgroundColor = [UIColor whiteColor];
    [self addLasttableView];
    [self passingData];
}
- (void)passingData{
 NSString *str = [NSString stringWithFormat:@"http://app.beva.com/box-playlistItemData/p46/pl%@/kwerge--v2.0/page1-rp1000.json",_zjId];
    [zJRequestManager requestWithUrl:str parmDic:nil method:GET succeedWithData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *alldic= dic[@"data"];
        NSArray *allArray = alldic[@"item"];
        _listArray = [NSMutableArray array ];
        for (NSDictionary *onedic in allArray) {
            MLZPlistLastModel *model = [[MLZPlistLastModel alloc]init];
            [model setValuesForKeysWithDictionary:onedic];
            [_listArray addObject:model];
        }
        [_tableView reloadData];
        
           } failedWithError:^(NSError *connectionError) {
        NSLog(@"%@",connectionError);
    }];









}
- (void)addLasttableView{

    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, KW, KH-64) style:(UITableViewStylePlain)];
    
    _tableView.delegate = self;
    
    _tableView.dataSource= self;
    
    _tableView.rowHeight = 150;
    
    [self.view addSubview:_tableView];


}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{



    return _listArray.count;


}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    static NSString *identifier = @"ZJCell";
    MLZPlayLastViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[MLZPlayLastViewCell alloc ]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    
    MLZPlistLastModel *model = _listArray[indexPath.row];
    
    cell.model =model;
    
    
    return cell;
    



}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    MLZPlistLastModel *model = _listArray[indexPath.row];
    _index= indexPath.row;
    NSURL *url = [NSURL URLWithString:model.mp4_720p.bcsurl];
    JRPlayerViewController *play = [[JRPlayerViewController alloc]initWithHTTPLiveStreamingMediaURL:url];
    NSMutableArray *playlist = [NSMutableArray array ];
    for (int i  = 0 ; i< _listArray.count; i++) {
        MLZPlistLastModel *model = _listArray[i];
        [playlist addObject:model.mp4_720p.bcsurl];
    }
    play.PlayList = playlist;
    play.index = indexPath.row;
    
    
    [self presentViewController:play animated:YES completion:nil];
    




}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
